package com.taotao.service.impl;

import com.taotao.common.pojo.TaotaoResult;
import com.taotao.mapper.TbItemMapper;
import com.taotao.mapper.TbItemParamItemMapper;
import com.taotao.pojo.TbItem;
import com.taotao.pojo.TbItemParam;
import com.taotao.pojo.TbItemParamItem;
import com.taotao.pojo.TbItemParamItemExample;
import com.taotao.pojo.TbItemParamItemExample.Criteria;
import com.taotao.service.ItemParamItemService;
import com.taotao.service.ItemParamService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ItemParamItemServiceImpl implements ItemParamItemService {
	@Autowired
	private TbItemParamItemMapper tbItemParamItemMapper;
	@Autowired
	private TbItemMapper tbItemMapper;
	@Autowired
	private ItemParamService itemParamService;

	public TaotaoResult getItemParamItemByItemId(long itemId) {
		long itemCid = getItemCidByItemId(itemId);
		TbItemParam tbItemParam = getTbItemParamByCid(itemCid);

		TbItemParamItemExample example = new TbItemParamItemExample();
		Criteria criteria = example.createCriteria();
		criteria.andItemIdEqualTo(itemId);
		List<TbItemParamItem> list = tbItemParamItemMapper
				.selectByExampleWithBLOBs(example);
		if ((list.size() > 0) && (list != null) && (tbItemParam != null)) {
			TbItemParamItem tbItemParamItem = (TbItemParamItem) list.get(0);
			return TaotaoResult.ok(tbItemParamItem);
		}
		if ((list.size() == 0) && (tbItemParam != null)) {
			return TaotaoResult.ok(tbItemParam);
		}
		return TaotaoResult.ok();
	}

	public long getItemCidByItemId(long itemId) {
		TbItem tbItem = this.tbItemMapper.selectByPrimaryKey(itemId);
		long itemCid = tbItem.getCid();
		return itemCid;
	}

	public TbItemParam getTbItemParamByCid(long itemCid) {
		TaotaoResult result = itemParamService.getItemParamByCid(itemCid);
		TbItemParam tbItemParam = (TbItemParam) result.getData();
		return tbItemParam;
	}
}
